package com.qianfeng.mybatis.suanfa;

import org.junit.Test;

import java.util.Arrays;

public class Sort {
    int[] ints={30,68,20,48,63,55};
    //        int[] ints={6,5,4,3,2,1};
    //冒泡排序，相邻元素做比较并交换
    @Test
    public void maoPao(){
        for (int i = 0; i < ints.length-1; i++) {
            for (int j = 0; j < ints.length-1; j++) {//i<5,01234
                if (ints[j]>ints[j+1]){
                    int temp=ints[j];
                    ints[j]=ints[j+1];
                    ints[j+1]=temp;
                }
            }
        }
        System.err.println(Arrays.toString(ints));
    }
    //选择排序，选取最大或最小排在首或末
    @Test
    public void xuanze(){
        int maxLocation=0;
        for (int i = 0; i < ints.length-1; i++) {
            //寻找最大值及位置
            int max=0;
            for (int j = 0; j < ints.length-i;j++) {
                if (max<ints[j]){
                    max=ints[j];
                    maxLocation=j;
                }
            }
            int temp=ints[ints.length-1-i];//将末位的值存起来
            ints[ints.length-1-i]=max;//将最大值赋给末位
            ints[maxLocation]=temp;//将原末位值给原最大值处
        }
        System.err.println(Arrays.toString(ints));
    }
}
